package com.ca.demo.common.base;

import com.ca.demo.common.base.annotation.CurrentUser;
import com.ca.demo.common.page.PageRequest;
import com.ca.demo.config.security.SecurityUser;
import com.ca.demo.model.sys.tmp.Menu;
import com.ca.demo.service.sys.ResourceService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.Serializable;
import java.util.List;

/**
 * @Author XieLei
 * @Date 2017/6/22 14:48
 */
public abstract class BaseCRUDController<T extends BaseEntity<ID>, ID extends Serializable> extends BaseController<T, ID> {

    @Autowired
    protected BaseService<T, ID> baseService;
    @Autowired
    private ResourceService resourceService;

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(Model model, @CurrentUser SecurityUser user) {
        List<Menu> menu = resourceService.findMenus(user);
        model.addAttribute("menu", menu);
        return viewName("index");
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    @ResponseBody
    public Response<PageInfo<T>> list(T example, PageRequest pageRequest) {
        PageInfo<T> pageInfo = baseService.findByPage(example, pageRequest);
        return new Response<>(pageInfo);
    }
}
